/*******************************************************************************
 * @(#)DatabaseHelper.java 2015年7月1日
 *
 * Copyright 2015 emrubik Group Ltd. All rights reserved.
 * EMRubik PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *******************************************************************************/
package com.inossem.utils.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.inossem.utils.log.LogUtils;

import java.util.List;
import java.util.Map;

import static com.inossem.utils.database.DatabaseUtils.FIRST_DATABASE_VERSION;

public class DatabaseHelper extends SQLiteOpenHelper {

    private List<String> mCreateSql;

    private Map<Integer, List<String>> mUpgradeSql;

    private DatabaseUtils.DatabaseResponse mDatabaseResponse;

    private String mDatabaseFileName;

    private int mDatabaseVersion;

    private Context mContext;

    private boolean failureHasCalled = false,successHasCalled = false;

    /**
     * 构造方法
     * @author leij
     * @param context 上下文
     * @param databaseFileName 数据库文件名
     * @param databaseVersion 数据库版本
     * @param createSql 创建数据库表的SQL语句数组
     * @param upgradeSql 更新数据库表的SQL语句数据
     */
    DatabaseHelper(Context context, String databaseFileName, int databaseVersion,
            List<String> createSql, Map<Integer, List<String>> upgradeSql,
            DatabaseUtils.DatabaseResponse databaseResponse) {
        super(context, databaseFileName, null, databaseVersion);
        this.mContext = context;
        this.mDatabaseFileName = databaseFileName;
        this.mDatabaseVersion = databaseVersion;
        this.mCreateSql = createSql;
        this.mUpgradeSql = upgradeSql;
        this.mDatabaseResponse = databaseResponse;
    }

    /**
     * 执行错误的回调
     * @author leij
     * @param databaseResponse 数据库相应对象监听
     * @param code 错误码
     */
    private void onFailure(DatabaseUtils.DatabaseResponse databaseResponse, int code) {
        if (databaseResponse != null && !failureHasCalled) {
            failureHasCalled = true;
            databaseResponse.onFailure(code);
        }
    }

    /**
     * 执行成功的回调
     * @author leij
     * @param databaseResponse 数据库相应对象监听
     */
    private void onSuccess(DatabaseUtils.DatabaseResponse databaseResponse) {
        if (databaseResponse != null && !successHasCalled) {
            successHasCalled = true;
            databaseResponse.onSuccess();
        }
    }

    /**
     * 第一次初始化数据库时调用此方法
     * @author leij
     * @param db SQLiteDatabase对象
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = 0; i < mCreateSql.size(); i++) {
            try {
                String sql = mCreateSql.get(i);
                db.execSQL(sql);
            } catch (Throwable throwable) {
                LogUtils.logThrowableE(mContext,"",throwable);
                onFailure(mDatabaseResponse, DatabaseUtils.CREATE_ERROR);
            }
        }
        LogUtils.logDatabaseI(mContext,"数据库版本【" + FIRST_DATABASE_VERSION + "】初始化成功");
        onUpgrade(db, FIRST_DATABASE_VERSION, mDatabaseVersion);
    }

    /**
     * 更新时调用此方法
     * @author leij
     * @param db SQLiteDatabase对象
     * @param oldVersion 旧版本
     * @param newVersion 新版本
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 使用for实现跨版本升级数据库
        try {
            for (int i = oldVersion; i < newVersion; i++) {
                List<String> upgradeSqlList = mUpgradeSql.get(i);
                for (String upgradeSql : upgradeSqlList) {
                    db.execSQL(upgradeSql);
                }
                LogUtils.logDatabaseI(mContext,"数据库版本【" + i + "】升级到【" + (i + 1) + "】成功");
            }
        } catch (Throwable throwable) {
            LogUtils.logThrowableE(mContext,"",throwable);
            onFailure(mDatabaseResponse, DatabaseUtils.UPGRADE_ERROR);
        }
        onSuccess(mDatabaseResponse);

    }

}
